package com.software.twinmanager.tcp;

import com.software.twinmanager.controller.MessageReceiver;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

@Slf4j
public class ReceiveMessageThread extends Thread{
    private InputStream is;
    private MessageReceiver server;

    public ReceiveMessageThread(InputStream is, MessageReceiver server) {
        this.is = is;
        this.server = server;
    }

    @Override
    public void run() {
        super.run();
        while (true) {
            byte[] message = new byte[8];
            try {
                is.read(message);
            } catch (IOException e) {
                TCPUtils.resetConnection();
                log.warn("TCP断开了与机器的连接");
                break;
            }

            server.appointSending("client", Arrays.toString(message));
        }
    }
}
